package com.onLineCar.pay.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.onLineCar.pay.entity.PayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private  HttpServletResponse response;

    @RequestMapping("/ailpay")
    public void show(@RequestBody PayRequest payRequest)
            throws IOException {
        AlipayClient alipayClient = new
                DefaultAlipayClient("https://openapi-sandbox.dl.alipaydev.com/gateway.do",
                "9021000132681369",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCPCCUk0CCC/Q5iKGVK2Cx+6R2xPafjATAzUhvQhJQJrnJPoyANyuWqQHW+6W59CvJmNyTeepZt0fF11NbQyUwDmnDKvMMA8Sf9iNuC20dR/JZ6/3Ot7J4SSL36jgk1PWiH8HiUi10CQr4v6Gcl/qROl1uXCWPyW9UMGZIjUDtzv0BBqcao61HDDIci4q3iInXqMOhd98a95qcR8YDJDJJb8FoJAc1vu72B9NmbMBrLDKqATQmFXv2RnnnMHNoemhzjd1l8cf0muJYFT5NxB/NiI/9xv6cZRqoJ0y/BQ/nUC6Rb1mYLKEmsNeIXqoKLgwX/6NIVqr9hkHUYtq1Drh0HAgMBAAECggEAOoMAfvDNfAh9q9YOqfGJMlm3Fdy9I3ZG+5epXUH/NX6xsLZw1HOeRGmeQX1I/8pMo1gHsyO/CioKd0OcpUjT2d2CKs3RkD8XNvaXpg6CPJ3yCVsbPZlAiQZNAYS9a5Q1gPb0vP/OCrC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jtrfCz2clSXZqkTyCeFI=",
                "json","utf-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjwglJNAggv0OYihlStgsfukdsT2n4wEwM1Ib0ISUCa5yT6MgDcrlqkB1vulufQryZjck3nqWbdHxddTW0MlMA5pwyrzDAPEn/YjbgttHUfyWev9zreyeEki9+o4JNT1oh/B4lItdAkK+L+hnJf6kTpdblwlj8lvVDBmSI1A7c79AQanGqOtRwwyHIuKt4iJ16jDoXffGveanEfGAyQySW/BaCQHNb7u9gfTZmzAaywyqgE0JhV79kZ55zBzaHpoc43dZfHH9JriWBU+TcQfzYiP/cb+nGUaqCdMvwUP51AukW9ZmCyhJrDXiF6qCi4MF/+jSFaq/YZB1GLatQ64dBwIDAQAB",
                "RSA2" );
        //上面这个方法是不是很眼熟？就是刚才配置完公钥后生成的那段，你可以直接复制然后加上你的私钥就好
        UUID uuid = UUID.randomUUID();
        String out_trade_no = payRequest.getOrderNum();//订单号
        out_trade_no = URLDecoder.decode(out_trade_no,"UTF-8");
        String total_amount = payRequest.getTotalAmount().toString();//总金额
        total_amount = URLDecoder.decode(total_amount,"UTF-8");
        String subject = payRequest.getOrderName();//商品名称
        subject = URLDecoder.decode(subject,"UTF-8");
        String body = payRequest.getOrderDesc();//商品描述
        body = URLDecoder.decode(body,"UTF-8");

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
        alipayRequest.setReturnUrl(payRequest.getReturnUrl());
        alipayRequest.setNotifyUrl(payRequest.getNotifyUrl());
        alipayRequest.setBizContent("{" +
                "    \"out_trade_no\":\""+ out_trade_no +"\"," +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "    \"total_amount\":"+ total_amount +"," +
                "    \"subject\":\""+ subject +"\"," +
                "    \"body\":\""+ body +"\"" +
                "    }"+
                "  }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println(form);//直接将完整的表单html输出到页面
        response.getWriter().close();
    }
    @RequestMapping("/success")
    public ModelAndView success(String out_trade_no, Double total_amount){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success");
        modelAndView.addObject("money",total_amount);
        modelAndView.addObject("no",out_trade_no);
        return modelAndView;
    }
    @RequestMapping("/hello")
    public void show(){
        System.out.println("ddd");
    }

    @RequestMapping("/notify")
    public void notify(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        //获取支付宝POST过来反馈信息
        Map<String,String> params = new HashMap<>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            System.out.printf("name:{}",name);
        }
    }
}
